package cn.fastoo.common.dao.member;
import cn.fastoo.common.entity.member.MemberGrade;
import cn.fastoo.common.sampleframe.dao.FastDao;
import cn.fastoo.common.sampleframe.dao.impl.FastDaoImpl;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class MemberGradeDao extends FastDaoImpl<MemberGrade> implements FastDao<MemberGrade> {


    public Logger logger = LoggerFactory.getLogger(MemberGradeDao.class);

    private List<MemberGrade> cache = null;
    private Map<Long,MemberGrade> map = null;

    @Autowired
    @Qualifier("mainDS")
    private void setJt(DataSource ds){
        this.jt = new JdbcTemplate(ds);
    }
    @Scheduled(cron = "0/3 * * * * ?")
    private void init(){
        cache = find(new Search());
        if(Collections3.isNotEmpty(cache)){
            map = new HashMap<>();
            cache.forEach(t->{
                map.put(t.getId(),t);
            });
        }
    }


    public List<MemberGrade> findAllOffical(){
        return find(new Search().addSearchFilter("EQ_isOfficial",1));
    }

    public String getNameById(Long id){
        MemberGrade memberGrade = map.get(id);
        if(memberGrade==null){
            return "";
        }
        return memberGrade.getName();
    }


}
